package com.example.chasshelper.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.chasshelper.R;
import com.example.chasshelper.adapter.ClassroomTeaAdapter;
import com.example.chasshelper.dao.ClassroomDao;
import com.example.chasshelper.dao.CourseDao;
import com.example.chasshelper.dao.MessageDao;
import com.example.chasshelper.dao.RegistrationDao;
import com.example.chasshelper.dao.SolutionDao;
import com.example.chasshelper.dao.StudentDao;
import com.example.chasshelper.dao.TeacherDao;
import com.example.chasshelper.dao.TestDao;
import com.example.chasshelper.model.Classroom;
import com.example.chasshelper.model.Course;
import com.example.chasshelper.model.Teacher;
import com.example.chasshelper.utils.DaoUtils;

import java.util.ArrayList;
import java.util.List;
/*
* 向ClassroomTeaAdapter传数据
* */
public class ClassroomTeaFragment extends Fragment implements View.OnClickListener{

    // TODO: Rename parameter arguments, choose names that match
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private View view;
    private Activity mActivity;
    private Context mContext;
    //控件
    private EditText et_factor;
    private ImageView iv_add;
    private ImageView iv_search;
    private RecyclerView rv_classroom_tea;
    //数据
    private List<Classroom> classroomList;
    private Teacher teacher;
    //dao
    private TeacherDao teacherDao= DaoUtils.getTeacherDao();
    private StudentDao studentDao=DaoUtils.getStudentDao();
    private CourseDao courseDao=DaoUtils.getCourseDao();
    private ClassroomDao classroomDao=DaoUtils.getClassroomDao();
    private RegistrationDao registrationDao=DaoUtils.getRegistrationDao();
    private SolutionDao solutionDao = DaoUtils.getSolutionDao();
    private MessageDao messageDao = DaoUtils.getMessageDao();
    private TestDao testDao = DaoUtils.getTestDao();
    //adapter
    private ClassroomTeaAdapter classroomTeaAdapter;
    public ClassroomTeaFragment() {
    }

    // TODO: Rename and change types and number of parameters
    public static ClassroomTeaFragment newInstance(String param1, String param2) {
        ClassroomTeaFragment fragment = new ClassroomTeaFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // 绑定布局
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_classroom_tea, container, false);
        }
        mActivity = getActivity();
        mContext = getContext();
        initUI();
        initData();
        return view;
    }

    private void initUI() {
        //绑定控件
        rv_classroom_tea = view.findViewById(R.id.rv_classroom_tea);
        et_factor = view.findViewById(R.id.et_factor);
        iv_search = view.findViewById(R.id.iv_search);
        //点击事件
        iv_search.setOnClickListener(this);
    }
    //接收并传递数据
    private void initData() {
        //接收数据
        mActivity = getActivity();
        mContext = getContext();
        Bundle bundle = this.getArguments();
        teacher = (Teacher)bundle.getSerializable("teacher");
        //dao获取数据
        classroomList = classroomDao.findByTeacherId(teacher);
        //adapter初始化，并向adapter传递数据
        classroomTeaAdapter = new ClassroomTeaAdapter(mContext,classroomList,teacher);
        rv_classroom_tea.setLayoutManager(new LinearLayoutManager(mContext));
        rv_classroom_tea.setAdapter(classroomTeaAdapter);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.iv_search://搜索
                classroomList = classroomDao.findByClassroomNameContainingAndTeacher_TeacherId(et_factor.getText().toString(),teacher);
                classroomTeaAdapter.setList(classroomList);
                classroomTeaAdapter.notifyDataSetChanged();
                break;
        }
    }
}